package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.device;

import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.DeviceMaintenanceMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Device;
import com.cqhilink.iot.jobstandardmanagement.facade.model.DeviceMaintenance;
import com.cqhilink.iot.jobstandardmanagement.facade.model.EmployeeVo;
import com.cqhilink.iot.jobstandardmanagement.facade.service.device.JsmDeviceMaintenanceService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by huangxueqian on 2017/5/5/005.
 * 设备的维保周期
 */
public class JsmDeviceMaintenanceServiceImpl implements JsmDeviceMaintenanceService {
    @Autowired
    private DeviceMaintenanceMapper deviceMaintenanceMapper;

    /**
     * @param device 设备实体
     * @param deviceMaintenance 维保周期实体
     * @param employee 操作人员实体
     * @return 操作数量
     * @throws Exception
     */
    @Override
    public int insertDeviceMaintenance(Device device, DeviceMaintenance deviceMaintenance, EmployeeVo employee) throws Exception {
        int resInt = 0;
        // 判断是否是新增 还是 修改
        if (deviceMaintenance.getMaintenanceId() == null) {
            deviceMaintenance.setMaintenanceId(UniqueUtil.uuid());
            deviceMaintenance.setStatus("0");
            deviceMaintenance.setDeviceId(device.getDeviceId());
            resInt = resInt + deviceMaintenanceMapper.insertSelective(deviceMaintenance);
        } else {
            resInt = deviceMaintenanceMapper.updateByPrimaryKeySelective(deviceMaintenance);
        }
        return resInt;
    }

    /**
     * @param device 设备实体
     * @param deviceMaintenances 维保周期内容
     * @param employee 操作人实体
     */
    @Override
    public int insertDeviceMaintenances(Device device, List<DeviceMaintenance> deviceMaintenances, EmployeeVo employee) throws Exception {
        int resInt = 0;
        // 判断维保周期是否为空
        if (deviceMaintenances != null && deviceMaintenances.size() > 0) {
            // 循环维保周期内容
            for (DeviceMaintenance deviceMaintenance : deviceMaintenances) {
                resInt = resInt + insertDeviceMaintenance(device, deviceMaintenance, employee);
            }
        }
        return resInt;
    }

    /**
     * 查询维保周期列表 ， 注意排序
     * @param deviceMaintenance 获取维保周期内容
     */
    @Override
    public List<DeviceMaintenance> getDeviceMaintenanceList(DeviceMaintenance deviceMaintenance) throws Exception {
        return deviceMaintenanceMapper.selectByDeviceMaintenanceList(deviceMaintenance);
    }

    /**
     * @param deviceMaintenances 删除维保周期之后进行更新
     * @param employeeVo 当前操作人信息
     * @return
     */
    @Override
    public int delDeviceMaintenances(List<DeviceMaintenance> deviceMaintenances, EmployeeVo employeeVo) {
        int resInt = 0;
        if (deviceMaintenances != null && deviceMaintenances.size() > 0) {
            for (DeviceMaintenance deviceMaintenance : deviceMaintenances) {
                resInt = resInt + deviceMaintenanceMapper.deleteByPrimaryKey(deviceMaintenance.getMaintenanceId());
            }
        }
        return resInt;
    }

    /**
     * @param device 删除维保周期
     * @return
     */
    @Override
    public int delDeviceMaintenances(Device device) {
        return deviceMaintenanceMapper.delDeviceMaintenances(device);
    }

    @Override
    public List<DeviceMaintenance> selectByDeviceMaintenanceList(DeviceMaintenance deviceMaintenance) throws Exception {
        return deviceMaintenanceMapper.getDeviceMaintenanceList(deviceMaintenance);
    }

}
